<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\MemberAccountType;
use app\model\Category;
use app\model\MemberAccountObtain;
use app\model\Goods;

class MemberaccounttypeController extends Base
{

	function index()
	{
		$weid = weid();
		$page = input('post.page', 1, 'intval');
		$keyword = input('post.keyword', '', 'serach_in');
		$status = input('post.status', '', 'serach_in');
		$query = MemberAccountType::where(['weid' => $weid]);
		if (!empty($keyword)) {
			$query->where('name', 'like', '%' . $keyword . '%');
		}

		if (!empty($status) || $status === "0") {
			$query->where(['status' => $status]);
		}
		if (!empty($this->sid)) {
			$query->where('sid', $this->sid);
		}

		$res = $query->order('sort asc,id asc')
			->paginate(getpage())
			->toArray();

		$data['data'] = $res;
		return $this->json($data);
	}

	function listUpdate()
	{
		$data = only('id,status,sort');
		if (!$data['id']) throw new ValidateException('参数错误');
		MemberAccountType::update($data);

		return $this->json(['msg' => '操作成功']);
	}

	public function update()
	{
		$id = $this->request->post('id');
		$data = input('post.');
		unset($data['create_time']);

		if (empty($id)) {
			$data['weid'] = weid();
			if (!empty($this->sid)) {
				$data['sid'] = $this->sid;
			}
			try {
				$res = MemberAccountType::create($data);
				if ($res->id && empty($data['sort'])) {
					MemberAccountType::update(['sort' => $res->id, 'id' => $res->id]);
				}
			} catch (\Exception $e) {
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '添加成功', 'data' => $res->id]);
		} else {
			try {
				MemberAccountType::update($data);

				if ($data['MemberAccountObtain']) {
					MemberAccountObtain::where('atid', $data['id'])->delete();
					if (isset($data['MemberAccountObtain'])) {
						foreach ($data['MemberAccountObtain'] as $mkoarr) {
							MemberAccountObtain::create([
								'atid' => (int) $data['id'],
								'calculate_method' => $mkoarr['calculate_method'],
								'ptype' => $mkoarr['ptype'],
								'return_percent' => $mkoarr['return_percent'],
                                'price' => $mkoarr['price'],
							]);
						}
					}
				}
			} catch (\Exception $e) {
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '修改成功']);
		}
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) throw new ValidateException('参数错误');
		$data = MemberAccountType::find($id)->toArray();

		if ($data['use_goods'] == 2 && !empty($data['goods_ids'])) {
			$goods = Goods::find($data['goods_ids']);
			if (!empty($goods)) {
				$data['goods'] = $goods->toArray();
			}
		} else {
			$data['goods'] = [];
		}

		$data['MemberAccountObtain'] = MemberAccountObtain::where(['atid' => $id])->select()->toArray();

		if (empty($data['MemberAccountObtain'])) {
			$data['MemberAccountObtain'] = [['calculate_method' => 0, 'ptype' => '', 'return_percent' => '']];
		}

		$data['getobtainPtypearray'] = getobtainPtype();


		return $this->json(['data' => $data]);
	}

	function delete()
	{
		return $this->del(new MemberAccountType());
	}
	function getField()
	{

		$data['expire_typearray'] = getExpireType();
		$data['cat_idsarray'] = Category::gettoparray();

		return $this->json(['data' => $data]);
	}
}
